<?php
class Textlink_model extends CI_Model
{
    public function __construct()
    {
        $this->load->model("widget_model");
        $this->load->database();
    }

    public function get_textlinks()
    {
        $widgets = $this->widget_model->get_widget_bytype('textlink');
        $result = array();
        if($widgets)
        {
            $widget_names = array();
            foreach($widgets as $key=>$value)
            {
                $widget_names[] = $value["name"];
            }
            $this->db->select('*');
            $this->db->from('t_textlink');
            $this->db->where_in('widget', $widget_names);
            $query = $this->db->get();
            $result =  $query->result_array();
        }
        return $result;
    }


    public function set_textlink()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'url' => $this->input->post('url'),
            'textvalue' => $this->input->post('textvalue'),
            'widget' => $this->input->post('widget')
        );

        return $this->db->insert('t_textlink',$data);
    }

    public function get_textlink_bywidget($widget)
    {
        $query = $this->db->get_where('t_textlink', array('widget' => $widget));
        return $query->result_array();
    }

    public function get_textlink_byid($id)
    {
        $query = $this->db->get_where('t_textlink', array('id' => $id));
        return $query->result_array();
    }

    public function delete_textlink($id)
    {
        $this->db->delete('t_textlink', array('id'=>$id));
    }

    public function delete_textlink_bywidget($widget)
    {
        $this->db->delete('t_textlink', array('widget'=>$widget));
    }

    public function update_textlink()
    {
        $data = array(
            'title' => $this->input->post('title'),
            'url' => $this->input->post('url'),
            'textvalue' => $this->input->post('textvalue')
        );

        $this->db->where('id', $this->input->post('id'));
        $this->db->update('t_textlink',$data);
    }
}
?>
